#!/usr/bin/env python
# -*- coding:utf-8 -*-
'''
@File    :   deep_copy.py
@Time    :   2024/11/21 19:44:38
@Author  :   xiangyu Chen
@Version :   1.0
@Contact :   yu112yu@sina.com
@License :   (C)Copyright 2017-2018, Liugroup-NLPR-CASIA
@Desc    :   None
'''

import copy
import json
import ctypes

a = [[["秀才", "芙蓉"], "张小鸡"], "姬无命"]

print("改变前,a的值", json.dumps(a, ensure_ascii=False))
print("改变前,a内部的元素id: id([a])->>>", [id(_) for _ in a])
print("改变前,a[0]内部的元素id: id([a])->>>", [id(_) for _ in a[0]])
print("改变前,a[0][1]内部的元素id: id([a])->>>", [id(_) for _ in a[0][1]])
d = copy.deepcopy(a)  #可变对象复制时均会生成新的地址

print("改变前,d的值", json.dumps(d, ensure_ascii=False))
print("改变前,深拷贝d内部的元素id: id([d])->>>", [id(_) for _ in d])
print("改变前,深拷贝d[0]内部的元素id: id([d])->>>", [id(_) for _ in d[0]])
print("改变前,深拷贝d[0][1]内部的元素id: id([d])->>>", [id(_) for _ in d[0][1]])
a[0][0][0] = "Tom"
a[1] = "Jack"

print("改变后,a的值", json.dumps(a, ensure_ascii=False))
print("改变后,d的值", json.dumps(d, ensure_ascii=False))
print("改变后,a内部的元素id: id([a])->>>", [id(_) for _ in a])
print("改变后,深拷贝d内部的元素id: id([d])->>>", [id(_) for _ in d])
print("改变后,a[0]内部的元素id: id([a])->>>", [id(_) for _ in a[0]])
# print("改变后,a[0]内部的元素: id([a])->>>", ctypes.cast([id(_) for _ in a[0]][1], ctypes.py_object).value) #从地址（id）获得地址对应的值
print("改变后,深拷贝d[0]内部的元素id: id([d])->>>", [id(_) for _ in d[0]])
# print("改变后,d[0]内部的元素: id([a])->>>", ctypes.cast([id(_) for _ in a[0]][1], ctypes.py_object).value)
print("改变后,a[0][1]内部的元素id: id([a])->>>", [id(_) for _ in a[0][1]])
print("改变后,深拷贝d[0][1]内部的元素id: id([d])->>>", [id(_) for _ in d[0][1]])
print("改变后,d[0]内部的元素: id([a])->>>", ctypes.cast([id(_) for _ in d[0][1]][0], ctypes.py_object).value)
print(a)
print(d)
print([id(_) for _ in '姬无命'])

